from browser import document
from browser import html
from browser import ajax
from browser import confirm
from browser import alert
import json
import time

groupManageBtn = document["groupManageBtn"]

groupTableTitle = ["Id","Names","HostsIDs","CreateDate","UpdateDate","Remask","Opeation"]

def whiteTitle(titleName):
    thead = html.THEAD()
    tr = html.TR()

    for i in range(len(titleName)):
        th = html.TH(titleName[i])
        tr <= th

    thead <= tr
    return thead

def delgroupComplete(req):
    alert(req.text)
    showGroupsInfos()

def delGroup(ev):
    btn = ev.target
    groupid = btn.dataset["groupid"]

    if confirm("确定删除组 " + groupid + " 信息么?"):
        data = {
            "delGroupID": groupid
        }

        request = ajax.Ajax()
        request.bind("complete", delgroupComplete)
        request.open("POST", "/manageGroups", False)
        request.send(data)

def showGroupsEv(req):
    if req.status == 200:
        data = json.loads(req.text)

        if 0 == data["Code"]:
            if None == data["Data"]:
                return

            tbody = html.TBODY()
            for line in data["Data"]:

                tr = html.TR()

                for k in groupTableTitle:
                    if k in line:
                        if k == "Id":
                            td = html.TH(line[k])

                        elif k == "CreateDate" or k == "UpdateDate":
                            td = html.TD(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(line[k]))))

                        else:
                            td = html.TD(line[k])

                    elif k == "Opeation":
                        UpdateA = html.A()
                        UpdateA.href = "./updateGroups.html?id=" + str(line["Id"])

                        UpdateBtn = html.BUTTON("update")
                        UpdateBtn.className = "btn btn-cyan btn-round btn-xs"

                        UpdateA <= UpdateBtn

                        DelBtn = html.BUTTON("delete")
                        DelBtn.className = "btn btn-danger btn-round btn-xs"
                        DelBtn.dataset["groupid"] = line["Id"]

                        DelBtn.bind("click", delGroup)

                        td = html.TD(UpdateA + " " + DelBtn)

                    tr <= td

                tbody <= tr
                groupTable <= tbody

def showGroupsInfos():
    workWindow.clear()

    addGroupA = html.A()
    addGroupA.href = "./addGroups.html"
    # addGroupA.target = "_blank"

    addGroupBtn = html.BUTTON("AddGroup")
    addGroupBtn.type = "button"
    addGroupBtn.className = "btn btn-primary btn-sm"

    addGroupA <= addGroupBtn

    workWindow <= html.DIV(addGroupA)

    global groupTable
    groupTable = html.TABLE()
    groupTable.className = "table table-hover"

    groupTable <= whiteTitle(groupTableTitle)
    workWindow <= groupTable

    request = ajax.Ajax()
    request.bind("complete", showGroupsEv)
    request.open("GET", "/showGroups", False)
    request.send()

def groupManage(ev):
    showGroupsInfos()


workWindow = document["workWindow"]

groupManageBtn.bind("click",groupManage)

